package com.wangzefeng.shop.controller;

import com.wangzefeng.shop.controller.base.BaseController;
import com.wangzefeng.shop.entity.SysUser;
import com.wangzefeng.shop.pojo.CommonResult;
import com.wangzefeng.shop.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Auther: wangzefeng
 * @Date: 2021/3/22 15:11
 * @Description:
 */
@RestController
@RequestMapping("/users")
public class UserController extends BaseController {

    @Autowired
    SysUserService sysUserService;

    @GetMapping("/list")
    public CommonResult getUserList(){
        List<SysUser> userList = sysUserService.getUserList(new HashMap<>());
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(userList);
        return successResult;
    }

    @GetMapping("/currentUser")
    public CommonResult currentUser(){
        return getCurrentUserInfo();
    }

    @GetMapping("/one/{userId}")
    public CommonResult getUserOne(@PathVariable("userId") int userId){
        SysUser user = sysUserService.getById(userId);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(user);
        return successResult;
    }

    @DeleteMapping("/delete/{userId}")
    public CommonResult deleteUserOne(@PathVariable("userId") int userId){
        boolean b = sysUserService.removeById(userId);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(b);
        return successResult;
    }

    @PutMapping("/edit")
    public CommonResult editUserOne(SysUser sysUser){
        if(sysUser.getUserId()==null)return CommonResult.getErrorResult(400,"未发现用户id，无法修改");
        boolean b = sysUserService.updateById(sysUser);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(b);
        return successResult;
    }

    @PostMapping("/save")
    public CommonResult saveUserOne(SysUser sysUser){
        if(sysUser.getUserId()!=null)return CommonResult.getErrorResult(400,"发生错误，无法保存");
        boolean b = sysUserService.save(sysUser);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(b);
        return successResult;
    }
}
